<?php 
class Country_Widget_ListingProfileController extends Engine_Content_Widget_Abstract{
    public function indexAction(){
    
    //GET VIEWER
    $viewer = Engine_Api::_()->user()->getViewer();
    $viewer_id = $viewer->getIdentity();

    if (empty($viewer_id)) {
        return $this->setNoRender();
    }
    
    $request = Zend_Controller_Front::getInstance()->getRequest();
    $this->_listingType = $request->getParam('listingObject');
    
    //GET LISTING TYPE ID
    $this->view->listingtype_id = $listingtype_id = $this->_listingType->listingtype_id;
    $this->view->listingtypeArray = $listingtypeArray = $this->_listingType;
    $this->view->listing_singular_uc = $listing_singular_uc = ucfirst($listingtypeArray->title_singular);
    $this->view->listing_singular_lc = $listing_singular_lc = strtolower($listingtypeArray->title_singular);
    $this->view->listing_plural_lc = $listing_plural_lc = strtolower($listingtypeArray->title_plural);
    $this->view->listing_plural_uc = $listing_plural_uc = ucfirst($listingtypeArray->title_plural);

    $authorizationCore = Engine_Api::_()->authorization();
    $allowCreate  = $authorizationCore->isAllowed("sitereview_listing",$viewer,"create_listtype_$listingtype_id");
    //CREATION PRIVACY CHECK
    if (empty($allowCreate)) {
        return $this->setNoRender();
    }

    //SEND LISTING TYPE TITLE TO TPL
    $this->view->title = ucfirst($this->_listingType->title_singular);

    //GET NAVIGATION
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation("sitereview_main_listtype_$listingtype_id");

    //GET EDIT AND DELETE SETTINGS
    $this->view->can_edit = $authorizationCore->isAllowed('sitereview_listing', $viewer, "edit_listtype_$listingtype_id");
    $this->view->can_delete = $authorizationCore->isAllowed('sitereview_listing', null, "delete_listtype_$listingtype_id");

    //ABLE TO UPLOAD VIDEO OR NOT        
    $this->view->allowed_upload_video = 1;
    $allowed_upload_videoEnable = Engine_Api::_()->sitereview()->enableVideoPlugin();
    if (empty($allowed_upload_videoEnable))
      $this->view->allowed_upload_video = 0;

    if (Engine_Api::_()->getApi('settings', 'core')->getSetting('sitereview.show.video', 1)) {
      //CHECK FOR SOCIAL ENGINE CORE VIDEO PLUGIN
      $allowed_upload_video_video = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'video', 'create');
      if (empty($allowed_upload_video_video))
        $this->view->allowed_upload_video = 0;
    }

    $allowed_upload_video = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'sitereview_listing', "video_listtype_$listingtype_id");
    if (empty($allowed_upload_video))
      $this->view->allowed_upload_video = 0;

    //ABLE TO ADD PHOTO OR NOT
    $this->view->allowed_upload_photo = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'sitereview_listing', "photo_listtype_$listingtype_id");

    //MAKE FORM
    $this->view->form = $form = new Sitereview_Form_Search();
    $form->removeElement('show');
   
    //PROCESS FORM
    unset($form->getElement('orderby')->options['']);
    if ($form->isValid($request->getParams())) {
      $values = $form->getValues();
    } else {
      $values = array();
    }

    //MAKE DATA ARRAY
    $values['user_id'] = $viewer_id;
    $values['type'] = 'manage';
    $values['listingtype_id'] = $listingtype_id;
    $values['orderby'] = 'listing_id';
    
    $request = Zend_Controller_Front::getInstance()->getRequest();
    $params = $request->getParams();

    $this->view->category_id = $values['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
    $this->view->subcategory_id = $values['subcategory_id'] = isset($params['subcategory_id']) ? $params['subcategory_id'] : 0;
    $this->view->subsubcategory_id = $values['subsubcategory_id'] = isset($params['subsubcategory_id']) ? $params['subsubcategory_id'] : 0;

    //GET CUSTOM FIELD VALUES
    $customFieldValues = array_intersect_key($values, $form->getFieldElements());

    //GET PAGINATOR
    $this->view->paginator = Engine_Api::_()->getDbTable('listings', 'sitereview')->getSitereviewsPaginator($values, $customFieldValues);
    $this->view->paginator->setItemCountPerPage(20);
    $this->view->paginator->setCurrentPageNumber($values['page']);
    $this->view->current_count = $this->view->paginator->getTotalItemCount();

    $this->view->formValues = $values;
    $form->populate($values);

    $categories = Engine_Api::_()->getDbTable('categories', 'sitereview')->getCategories(null, 0, $listingtype_id, 0, 1);
    $categories_slug[0] = "";
    if (count($categories) != 0) {
      foreach ($categories as $category) {
        $categories_slug[$category->category_id] = $category->getCategorySlug();
      }
    }
    $this->view->categories_slug = $categories_slug;

    //MAXIMUM ALLOWED LISTINGS
    $this->view->quota = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'sitereview_listing', "max_listtype_$listingtype_id");

    $this->view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');
    }
}
?>